// TestCollection.cs
//

using System;
using System.Runtime.CompilerServices;

namespace UnitTestHarness
{

    public sealed class TestCollection
    {
        public string CollectionName = "";
        public TestSuite[] Suites = (TestSuite[]) new Array();
        public static int RunMark;

        private Test _Test = null;
        private bool _SuiteChanged = true;
        public int SuiteIndex = 0;
        public int TestIndex = 0;
        public int completed;

        public Test FirstTest()
        {
            return NextTest(true);
        }

        [AlternateSignature]
        public extern Test NextTest();

        public Test NextTest(bool first)
        {
            if (first == true)
            {
                RunMark = (new Date()).GetTime();
                TestIndex = SuiteIndex = 0;
            }
            bool found = Type.GetScriptType(first) == "boolean" ? first : false;
            foreach (TestSuite suite in Suites)
            {
                TestIndex = 0;
                if (suite.completed == RunMark)
                    continue;
                for (int i = 0; i < suite.Tests.Length; i++)
                {
                    TestIndex = i;
                    TestTest tt = suite.Tests[i];
                    Test test = tt.test;

                    if (i == 0)
                        _SuiteChanged = true;
                    else
                        _SuiteChanged = false;

                    if (found)
                    {
                        if (tt.completed == RunMark)
                            continue;
                        _Test = test;
                        return test;
                    }
                    if ((bool)Script.Literal("{0} === {1}", _Test, test))
                    {
                        found = true;
                        tt.completed = RunMark;
                    }
                }
                found = true;
                SuiteIndex++;
                suite.completed = RunMark;
            }
            completed = RunMark;
            return null;
        }

        public bool SuiteChanged
        {
            get
            {
                return _SuiteChanged;
            }
        }

    }

    public class TestSuite : TestBase
    {
        public int completed;
        public TestTest[] Tests;
    }

    public class TestTest
    {
        public int completed;
        public Test test;

        public TestTest(Test _test)
        {
            test = _test;
        }
    }

    public delegate void Test();

}